<?php
class loginControl extends Control {

    //载入登录页面
    function index() {
        // C("debug",1);
        $this->display("login");
    }

    //取得验证码
    function getCode() {
        $code = new code();
        $code->show();
    }

    //接受表单里的数据并异步验证
    function checklogin(){
            if (strtolower($_SESSION['code']) != strtolower($_POST['code'])) {
                    echo json_encode(array('status' => 0, 'code'=>'验证码错误'));
                    die;
             }
            $username = $_POST['name'];
            $pwd =$_POST['pwd'];
            $db=m("user");
            $row=$db->all();
            foreach ($row as $v) {
                if ($v['username'] == $username) {
                        if (!$v['role']) {
                            echo json_encode(array('status' => 5, 'yes' => '您没有访问后台权限'));
                                die;
                        }
                        if ($v['lock']) {
                            echo json_encode(array('status' => 4, 'yes' => '该用户已被锁定，请联系管理员'));
                                die;
                        }
                        if ($v['password'] == $pwd) {
                                $_SESSION['easyuser']=$_POST['name'];
                                // setcookie("easyuser",$_POST['name'],0,"/");
                                echo json_encode(array('status' => 1, 'yes' => '登录成功'));
                                die;
                        } else {
                                echo json_encode(array('status' => 2, 'pwd' => '密码错'));
                                die;
                        }
                }
            }
            echo json_encode(array('status' => 3, 'user' => '用户不存在'));
    }
    

    //退出登录
    function logout(){
       // unset($_SESSION['user']);
        session_destroy();
        go("index/index");
    }




}

?>
